<html>
   <body>
      <div>
         <div><textarea id="minions" rows=10 style="width:100%;"
            placeholder="name/atk/hp/3SPRWT/deathrattleName/name*3/..."># demo data
# click "Go Fight!" to see result
# nameOrId/atk/hp/3SPRWT/...deathrattles
# e.g. # rat pack; atk=2, hp=2
#      rat pack
#      # rat pack; atk=7, hp=7
#      rat pack/7/7
#      # atk=32, hp=46, divineShield, Poisonous,
#      # *2 seeds (#seeds = -1 -> deathrattle: 2 plants)
#      murloc tidecaller/32/46/SP/-1/#seeds
#      # deflect-o-bot = 306, atk=10, hp=4, no additional attribute,
#      # *1 tri-replicatingMenace, *1 replicatingMenace
#      306/10/4//316*3/316
612
411/2/7
503
614/12/6/SW
611
312/7/7/3
-
306/5/6
306/5/8
302/3/2
302
406
107/8/20
415/4/14/ST</textarea></div>
         <button id="fight" >Go Fight!</button>
         <button id="lookupna" >N/A</button>
         <button id="lookup1" >1</button>
         <button id="lookup2" >2</button>
         <button id="lookup3" >3</button>
         <button id="lookup4" >4</button>
         <button id="lookup5" >5</button>
         <button id="lookup6" >6</button>
         <button id="lookupother" >other</button>
         <div id="lookup"></div>
      </div>
      <div id="result" style="width: 100%; margin-top: 5px;"></div>
      <style>
         table td { border-top: 1px solid black; }
      </style>
      <script type="text/javascript" src="pool-20200731.js"></script>
      <script type="text/javascript" src="deck.js"></script>
      <script>
         function lookup(evt) {
            var ui = document.querySelector('#lookup');
            ui.innerHTML = '';
            var list = [];
            switch(evt.target.getAttribute('id')) {
               case 'lookupna': return;
               case 'lookup1': list = G.pool.filter(function (x) { return ~~(x.id/100) === 1; }); break;
               case 'lookup2': list = G.pool.filter(function (x) { return ~~(x.id/100) === 2; }); break;
               case 'lookup3': list = G.pool.filter(function (x) { return ~~(x.id/100) === 3; }); break;
               case 'lookup4': list = G.pool.filter(function (x) { return ~~(x.id/100) === 4; }); break;
               case 'lookup5': list = G.pool.filter(function (x) { return ~~(x.id/100) === 5; }); break;
               case 'lookup6': list = G.pool.filter(function (x) { return ~~(x.id/100) === 6; }); break;
               case 'lookupother': list = G.pool.filter(function (x) { return ~~(x.id/100) < 1 && x.id > 0; }); break;
            }
            list.forEach(function (m) {
               var text = m.toString();
               var div = document.createElement('div');
               div.appendChild(document.createTextNode(text));
               ui.appendChild(div);
            });
         }
         document.querySelector('#lookupna').addEventListener('click', lookup);
         document.querySelector('#lookup1').addEventListener('click', lookup);
         document.querySelector('#lookup2').addEventListener('click', lookup);
         document.querySelector('#lookup3').addEventListener('click', lookup);
         document.querySelector('#lookup4').addEventListener('click', lookup);
         document.querySelector('#lookup5').addEventListener('click', lookup);
         document.querySelector('#lookup6').addEventListener('click', lookup);
         document.querySelector('#lookupother').addEventListener('click', lookup);

         function parseMinonText() {
            var text = document.querySelector('#minions').value;
            if (!text) return null;
            var slotA = [], slotB = [], slot = slotA;
            text.split('\n').forEach(function (line) {
               line = line.trim();
               if (!line) return;
               if (line.startsWith('#')) return;
               if (line === '-') {
                  slot = slotB;
                  return;
               }
               var args = line.split('/');
               // name|id/atk/hp/attr(3SPRWT, 3=tri, S=divineShield, P: poisonous, R: reborn, W: windfury, T: taunt)/additional_deathrattle(name,name*3)/...
               var nameOrId = args[0];
               var m = getTemplate(nameOrId);
               if (!m) return;
               if (args.length < 3) {
                  slot.push(m);
                  return;
               }
               if (args[3] && args[3].indexOf('3') >= 0) {
                  m = api.newMinionById(m.id, true);
               }
               m = parseValue(m, args);
               args.slice(4).forEach(function (nameOrId) {
                  if (!nameOrId) return;
                  var d = getTemplate(nameOrId);
                  if (!d) return;
                  var tri = false;
                  if (nameOrId.endsWith('*3')) tri = true;
                  d = api.newMinionById(d.id, tri);
                  if (!m.flags.deathrattle) m.flags.deathrattle = [];
                  m.flags.deathrattle.push(d);
               });
               if (!m) return;
               slot.push(m);

               function getTemplate(nameOrId) {
                  var m = G.pool.filter(function (x) { return x.name === nameOrId; })[0];
                  if (!m) {
                     var id = parseInt(nameOrId, 10);
                     m = G.pool.filter(function (x) { return x.id === id; })[0];
                     if (!m) {
                        m = new Minion(id, 0, 0, 0, {});
                     }
                  }
                  return m.clone();
               }

               function parseValue(m, args) {
                  m.atk = parseInt(args[1], 10);
                  m.hp = parseInt(args[2], 10);
                  if (m.hp <= 0) return null;
                  var attr = args[3]?args[3].split(''):null;
                  if (attr) attr.forEach(function (ch) {
                     switch (ch) {
                        case 'S': m.flags.shield = true; break;
                        case 'P': m.flags.poison = true; break;
                        case 'R': m.flags.reborn = true; break;
                        case 'W': m.flags.windfury = true; break;
                        case 'T': m.flags.taunt = true; break;
                     }
                  });
                  return m;
               }
            });
            return { A: slotA, B: slotB };
         }

         document.querySelector('#fight').addEventListener('click', function () {
            var slot = parseMinonText();
            if (!slot) return;
            goFight(slot.A, slot.B);
         });

         function goFight(slotA, slotB) {
            var result = document.querySelector('#result');
            result.innerHTML = '';
            var table = document.createElement('table');
            var tbody = document.createElement('tbody');
            for (var i = 0, n = Math.max(slotA.length, slotB.length); i < n; i++) {
               var x = slotA[i];
               var y = slotB[i];
               var tr = document.createElement('tr');
               var td = document.createElement('td');
               if (x) {
                  td.appendChild(document.createTextNode(x.toString()));
               } else {
                  td.innerHTML = '-';
               }
               tr.appendChild(td);
               td = document.createElement('td');
               if (y) {
                  td.appendChild(document.createTextNode(y.toString()));
               } else {
                  td.innerHTML = '-';
               }
               tbody.appendChild(tr);
               tr.appendChild(td);
            }
            table.appendChild(tbody);

            // to make sure when meet bugs
            // we can see what happens
            // comment out for faster performance
            T.debug = true;
            var res = { a: 0, b: 0, tie: 0, ad: 0, bd: 0 };
            var n = 10000;
            for (var i = 1; i < n; i ++) {
               var r = G.sandbox(slotA, slotB);
               if (r[0].length > 0 && r[1].length > 0) res.tie ++;
               else if (r[0].length > 0) {
                  res.a ++;
                  res.ad += r[0].map(function (x) {
                     var d = ~~(x.id/100);
                     if (d < 0 || d === 0) d = 1;
                     return d;
                  }).reduce(function (x, y) { return x+y; });
               } else if (r[1].length > 0) {
                  res.b ++;
                  res.bd += r[1].map(function (x) {
                     if (!x) return 0;
                     var d = ~~(x.id/100);
                     if (d < 0 || d === 0) d = 1;
                     return d;
                  }).reduce(function (x, y) { return x+y; });
               } else {
                  res.tie ++;
               }
            }

            var lastR = G.sandbox(slotA, slotB);
            if (lastR[0].length > 0 && lastR[1].length > 0) res.tie ++;
            else if (lastR[0].length > 0) res.a ++;
            else if (lastR[1].length > 0) res.b ++;
            else res.tie ++;

            var statTr, statTd;
            statTr = document.createElement('tr');
            statTd = document.createElement('td');
            statTd.colSpan = '2';
            statTd.innerHTML = 'Battle Statistics<br/>';
            statTd.appendChild(document.createTextNode(
               'SlotA win: ' + (Math.floor(res.a / n * 10000) / 100) + '% (' + (Math.floor(res.ad / res.a * 100) / 100) +'), ' +
               'SlotB win: ' + (Math.floor(res.b / n * 10000) / 100) + '% (' + (Math.floor(res.bd / res.b * 100) / 100) +'), ' +
               'Tie: ' + (Math.floor(res.tie / n * 10000) / 100) + '%'
            ));
            statTr.appendChild(statTd);
            tbody.appendChild(statTr);
            result.appendChild(table);
            result.appendChild(document.createElement('br'));

            table = document.createElement('table');
            table.innerHTML = (
               '<thead><tr><th>Attacker</th><th>Defenser</th>' +
               '<th>1</th><th>2</th><th>Memo</th></tr></thead>'
            );
            tbody = document.createElement('tbody');
            T.steps.forEach(function (step) {
               var tr, td;
               tr = document.createElement('tr');
               td = document.createElement('td');
               td.appendChild(document.createTextNode(step.a.toShortString()));
               tr.appendChild(td);
               td = document.createElement('td');
               td.appendChild(document.createTextNode(step.d.toShortString()));
               tr.appendChild(td);
               td = document.createElement('td');
               td.appendChild(document.createTextNode(step.f.map(function (m) { return m.toShortString(); }).join(', ')));
               tr.appendChild(td);
               td = document.createElement('td');
               td.appendChild(document.createTextNode(step.e.map(function (m) { return m.toShortString(); }).join(', ')));
               tr.appendChild(td);
               td = document.createElement('td'); td.innerHTML = '-'; tr.appendChild(td);
               tbody.appendChild(tr);
            });
            {
               var tr, td;
               tr = document.createElement('tr');
               td = document.createElement('td');
               td.colSpan = 5;
               td.appendChild(document.createTextNode(
                  'Final: ' + 
                  (lastR[0].map(function (m) { return m.toShortString(); }).join(', ') || '-') +
                  ' / ' +
                  (lastR[1].map(function (m) { return m.toShortString(); }).join(', ') || '-')
               ));
               tr.appendChild(td);
               tbody.appendChild(tr);
            }
            table.appendChild(tbody);
            result.appendChild(table);
         }
      </script>
   </body>
</html>
